import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_page/mine_business/mine_video_cache/video_cache_cell.dart';
import 'package:xvideos/hj_page/mine_business/mine_video_cache/video_cache_logic.dart';
import 'package:xvideos/routers/jump_router.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';

import '../../../hj_model/video_model.dart';
import '../../../tools_base/indicator/custom_tab_indicator.dart';
import '../../video/simple_video_player_page.dart';

class VideoCachePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<VideoCacheLogic>(
      init: VideoCacheLogic(),
      global: false,
      builder: (logic) {
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text("下载缓存"),
            actions: [
              InkWell(
                onTap: () => logic.editEvent(),
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 12),
                  alignment: Alignment.centerRight,
                  child: Text(
                    logic.isEditing ? "删除" : "编辑",
                    style: const TextStyle(
                      color: Color(0xff757575),
                      fontSize: 14,
                    ),
                  ),
                ),
              ),
              const SizedBox(width: 14),
            ],
          ),
          body: Obx(
            () {
              if (logic.isLoadingData.value) {
                return LoadingCenterWidget();
              } else {
                return Column(
                  children: [
                    Container(
                      color: Theme.of(context).appBarTheme.backgroundColor,
                      child: TabBar(
                        padding: EdgeInsets.only(left: 8),
                        tabAlignment: TabAlignment.center,
                        indicator: CustomIndicator(
                          width: 16,
                          height: 3,
                          useGradient: true,
                          gradientColors: [Color(0x00E1351F), Color(0xffE1351F)],
                        ),
                        indicatorWeight: 1,
                        unselectedLabelColor: Color(0x8CFFFFFF),
                        unselectedLabelStyle: TextStyle(fontSize: 14),
                        labelStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
                        labelColor: Color(0xE5FFFFFF),
                        isScrollable: true,
                        tabs: logic.typeTitle
                            .map(
                              (e) => Padding(
                                padding: EdgeInsets.fromLTRB(18, 5, 18, 5),
                                child: Text(e),
                              ),
                            )
                            .toList(),
                        controller: logic.controller,
                      ),
                    ),
                    Expanded(
                      child: TabBarView(
                        controller: logic.controller,
                        children: [
                          _buildLongTable(logic, 0),
                          _buildLongTable(logic, 1),
                          _buildLongTable(logic, 2),
                        ],
                      ),
                    )
                  ],
                );
              }
            },
          ),
        );
      },
    );
  }

  Widget _buildLongTable(VideoCacheLogic logic, int type) {
    List<VideoModel> dataArr = [];
    if(type == 0){
      dataArr = logic.longList;
    }else if(type == 1){
      dataArr = logic.shortList;
    }else {
      dataArr = logic.cartoonList;
    }
    if (dataArr.isEmpty) {
      return CErrorWidget();
    }
    return GridView.builder(
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        crossAxisSpacing: 6,
        mainAxisSpacing: 12,
        childAspectRatio: 191 / 210,
      ),
      itemCount: dataArr.length,
      itemBuilder: (context, int index) {
        final videoModel = dataArr[index];
        return GestureDetector(
          onTap: () {
            if (videoModel.id == "-1") {
              Get.to(SimpleVideoPlayerPage(
                localPath: videoModel.localPath,
                videoUrl: videoModel.realVideoUrl,
                title: videoModel.title ?? "",
              ));
            } else {
              pushToVideoPage(videoModel: videoModel, isCartoon: videoModel.videoType == 1);
            }
          },
          child: VideoCacheCell(
            videoModel: videoModel,
            isCacheEdit: logic.isEditing,
            onCacheTap: () => logic.loadStartOrStopEvent(videoModel),
          ),
        );
      },
    );
  }
}
